package com.future.hedongjun.mine.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.FileUtils;
import com.future.baselib.activity.BaseActivity;
import com.future.baselib.entity.BaseResponse;
import com.future.baselib.http.HttpRequest;
import com.future.baselib.view.IosAlertDialog;
import com.future.hedongjun.R;
import com.future.hedongjun.common.Contant;
import com.future.hedongjun.common.MyApp;
import com.future.hedongjun.mine.MsgPushPreference;
import com.future.hedongjun.mine.bean.response.UserInfoResponse;
import com.kyleduo.switchbutton.SwitchButton;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

public class SettingActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener {

    @BindView(R.id.tv_edit_pwd)
    TextView tvEditPwd;
    @BindView(R.id.tv_user_protocol)
    TextView tvUserProtocol;
    @BindView(R.id.sb_message)
    SwitchButton sbMessage;
    @BindView(R.id.setting_tv_huancun)
    TextView settingTvHuancun;
    @BindView(R.id.setting_layout_clear)
    LinearLayout settingLayoutClear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        setTitle("设置");
        boolean canPush = MsgPushPreference.getInstance(this).canPush();
        sbMessage.setChecked(canPush);
        sbMessage.setOnCheckedChangeListener(this);
        String dirSize = FileUtils.getDirSize(Contant.CACHE_FILE);
        settingTvHuancun.setText(TextUtils.isEmpty(dirSize) ? "0M" : dirSize);
    }

    @OnClick({R.id.tv_edit_pwd, R.id.tv_user_protocol, R.id.setting_layout_clear})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_edit_pwd:
                Bundle bundle = new Bundle();
                bundle.putInt(EditPhone1Activity.KEY_TYPE, 2);
                startActivity(EditPhone1Activity.class, bundle);
                break;
            case R.id.tv_user_protocol:
                startActivity(UserProtocolActivity.class);
                break;
            case R.id.setting_layout_clear:
                new IosAlertDialog(this)
                        .builder()
                        .setMsg("确定清除缓存吗？\n(清除缓存后,音频和视频缓存\n需要重新下载哦)")
                        .setNegativeButton("取消", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {

                            }
                        })
                        .setPositiveButton("确定", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                boolean b = FileUtils.deleteAllInDir(Contant.CACHE_FILE);
                                if (!b) {
                                    toast("清除失败，请重试");
                                } else {
                                    String dirSize = FileUtils.getDirSize(Contant.CACHE_FILE);
                                    settingTvHuancun.setText(TextUtils.isEmpty(dirSize) ? "0M" : dirSize);
                                }
                            }
                        }).show();
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        MsgPushPreference.getInstance(this).canPush(isChecked);
        if (isChecked) {
            JPushInterface.resumePush(this);
            JPushInterface.setAlias(this, 0, MyApp.getInstance().getUserToken());
        } else {
            JPushInterface.stopPush(this);
        }

    }
}
